package com.manage.mynet.service;

import org.apache.log4j.Logger;
import org.xvolks.jnative.JNative;
import org.xvolks.jnative.Type;

public class Speak {

	private static Logger logger = Logger.getLogger(Speak.class);

	// 静态代码块加载后该值就需要改变
	private static int index = -1;

	private Speak() {
	}
	
	public static int getIndex() {
		return index;
	}
	/**
	 * 初始化验证码dll信息
	 * 
	 * 不使用静态代码块,只有使用到时候才加载
	 * 
	 * 如果使用验证码,最好在系统启动时候加载,如果不使用,则不需要加载
	 * 
	 */
	//public native long Sp_Initial(long SN1, long SN2, long SN3, long SN4, long SN5);
	public synchronized static void initial() {
		if (index < 0) {
			logger.info("加载验证码识别DLL文件信息");
			try {
				JNative n = new JNative("speak.dll", "Sp_Initial");
				n.setRetVal(Type.INT);
				n.setParameter(0, Type.LONG,"0");
				n.setParameter(1, Type.LONG,"0");
				n.setParameter(2, Type.LONG,"0");
				n.setParameter(3, Type.LONG,"0");
				n.setParameter(4, Type.LONG,"0");
				n.invoke();
				//index = Integer.parseInt(n.getRetVal());
				String res = n.getRetVal();
				//logger.info("验证码识别DLL初始化结果为:" + (index < 0 ? "失败" : "成功"));
				logger.info("验证码识别DLL初始化结果为:" + res);
			} catch (Exception e) {
				index = -1;
				logger.error("加载验证码识别DLL文件信息", e);
			}
		} else {
			logger.info("验证码识别DLL已经加载过,不用重复加载[index|" + index + "]");
		}
	}
	
	
	
	public synchronized static void speaktext(){
		if (index < 0) {
			logger.info("加载验证码识别DLL文件信息");
			try {
				JNative n = new JNative("speak.dll", "Sp_Speak");
				String pszText = "我是鲍秦";
				n.setRetVal(Type.INT);
				n.setParameter(0, Type.STRING,pszText);
				n.invoke();
				//index = Integer.parseInt(n.getRetVal());
				String res = n.getRetVal();
				//logger.info("验证码识别DLL初始化结果为:" + (index < 0 ? "失败" : "成功"));
				logger.info("语音朗读指定的文字成功:" + res);
			} catch (Exception e) {
				index = -1;
				logger.error("加载验证码识别DLL文件信息", e);
			}
		} else {
			logger.info("验证码识别DLL已经加载过,不用重复加载[index|" + index + "]");
		}
	}
	
	
	public synchronized static void waveOut(){
		if (index < 0) {
			logger.info("加载验证码识别DLL文件信息");
			try {
				JNative n = new JNative("speak.dll", "Sp_WaveOut");
				String pszText = "woshibaoq";
				String pszPath = "D:\\speakfile\\s1.wav";
				n.setRetVal(Type.INT);
				n.setParameter(0, Type.STRING,pszText);
				n.setParameter(1, Type.STRING,pszPath);
				n.invoke();
				//index = Integer.parseInt(n.getRetVal());
				String res = n.getRetVal();
				//logger.info("验证码识别DLL初始化结果为:" + (index < 0 ? "失败" : "成功"));
				logger.info("导出语音朗读成功:" + res);
			} catch (Exception e) {
				index = -1;
				logger.error("加载验证码识别DLL文件信息", e);
			}
		} else {
			logger.info("验证码识别DLL已经加载过,不用重复加载[index|" + index + "]");
		}
	}
	
	
	public synchronized static void setVoice(){
		if (index < 0) {
			logger.info("加载验证码识别DLL文件信息");
			try {
				JNative n = new JNative("speak.dll", "Sp_SetVoice");
				n.setRetVal(Type.INT);
				String pszAttribs = "Language=804;Gender=Male;Name=Sam";
				n.setParameter(1, Type.STRING,pszAttribs);
				n.invoke();
				//index = Integer.parseInt(n.getRetVal());
				String res = n.getRetVal();
				//logger.info("验证码识别DLL初始化结果为:" + (index < 0 ? "失败" : "成功"));
				logger.info("设置朗诵者:" + res);
			} catch (Exception e) {
				index = -1;
				logger.error("加载验证码识别DLL文件信息", e);
			}
		} else {
			logger.info("验证码识别DLL已经加载过,不用重复加载[index|" + index + "]");
		}
	}
	
}
